#ifndef HTTP_HEADER_BUILDER_HELPER_H_
#define HTTP_HEADER_BUILDER_HELPER_H_
#include <fermat/http/http_message_builder.h>
#include <fermat/common/string.h>
#include <string>

namespace fermat {
namespace http {

class HeaderBuilderHelper {
public:
	HeaderBuilderHelper(MessageBuilder *msg):_msg(msg){}

	void setContentLength(size_t s)
	{
		fermat::http::Message::Headers& h = _msg->headers();
		fermat::http::Message::Headers::iterator it = h.find("Content-Length");
		if(it != h.end()) {
			it->second = fermat::to_str(s);
			return;
		}

		std::pair<std::string,std::string> ent("Content-Length", fermat::to_str(s));

		h.insert(ent);	
	}

	void setContentType(const std::string & type)
	{

		fermat::http::Message::Headers& h = _msg->headers();
		fermat::http::Message::Headers::iterator it = h.find("Content-Type");
		if(it != h.end()) {
			it->second = type;
			return;
		}
		std::pair<std::string,std::string> ent("Content-Type", type);

		h.insert(ent);	
	}

	void setServer(const std::string &server)
	{
		fermat::http::Message::Headers& h = _msg->headers();
		std::pair<std::string,std::string> ent("Server", server);

		h.insert(ent);	
	}

	void setHost(const std::string &host)
	{

		fermat::http::Message::Headers& h = _msg->headers();
		std::pair<std::string,std::string> ent("Host", host);
		h.insert(ent);	

	}
	void setHeader(const std::string &key, const std::string &value)
	{
		fermat::http::Message::Headers& h = _msg->headers();
		std::pair<std::string,std::string> ent(key, value);

		h.insert(ent);	
	}
	void setKeepAlive(bool on)
	{
		std::string kp = "Keep-Alive";
		if(!on) {
			kp = "Close";
		}
		fermat::http::Message::Headers& h = _msg->headers();
		fermat::http::Message::Headers::iterator it = h.find("Connection");
		if(it != h.end()) {
			it->second = kp;
			return;
		}
		std::pair<std::string,std::string> ent("Connection", kp);

		h.insert(ent);		
	}


private:
	MessageBuilder *_msg;
};
}
}

#endif